<template>
  <div class="register">
    <div class="title">
      <p>注册页面</p>
    </div>
    <div class="zh">
      <input
        type="text"
        v-model="userRegister.phone"
        placeholder="手机/用户名"
      />
      <input type="password" v-model="userRegister.pass" placeholder="密码" />
      <input
        type="password"
        v-model="userRegister.repass"
        placeholder="确认密码"
      />
    </div>
    <div class="dl">
      <input type="button" @click="register" value="注册" />
    </div>
  </div>
</template>

<script>
import { user_register } from "../utils/api";
export default {
  data() {
    return {
      userRegister: {
        phone: "",
        pass: "",
        repass: "",
      },
    };
  },
  methods: {
    register() {
      // console.log(
      //   this.userRegister.phone,
      //   this.userRegister.pass,
      //   this.userRegister.repass
      // );
      if (/^1[3|5|7]\d{9}$/.test(this.userRegister.phone) == false) {
        alert("请输入正确的手机号");
      } else if (/^\d{6}$/.test(this.userRegister.pass) == false) {
        alert("请输入正确密码");
      } else if (this.userRegister.pass !== this.userRegister.repass) {
        alert("保证输入的密码和重新输入的密码一致");
      } else {
        var newuser = JSON.parse(JSON.stringify(this.userRegister));
        delete newuser.repass;
        // 查看删除后的元素
        console.log(newuser);
        user_register(newuser).then((res) => {
          // console.log(res.data.code); //返回的数据是400，成功是200
          console.log(res.data);
          if (res.data.code == 200) {
            alert("注册成功");
            this.$router.push("/login");
            //如果注册成功点击注册之后保存用户名和密码到vuex
            this.$store.commit("user_Collect", newuser);
          } else {
            alert(res.data.msg);
          }
        });
      }
    },
  },
};
</script>

<style lang='scss' scoped>
.register {
  padding: 20px;
  .title {
    font-size: 30px;
  }
  .zh {
    margin-top: 20px;
    input {
      height: 40px;
      width: 100%;
      margin-top: 30px;
      outline: none;
      border: none;
      border-bottom: 1px solid #999;
    }
  }
  .dl {
    input {
      margin-top: 40px;
      width: 100%;
      height: 40px;
      outline: none;
      border: none;
      border-radius: 20px;
    }
  }
  .mm {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    span {
      color: aqua;
    }
  }
}
</style>